package com.wk.service;

import com.wk.bean.*;
import com.wk.dao.BreakdownMapper;

import com.wk.dao.PatientMapper;
import com.wk.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

//@Component
@Service //告诉spring应用，我要把这个类创建对象放置spring容器
public class UserServiceImpl implements  UserService{
    @Autowired// 依赖注入,我从容器里面取东西，对象
    UserMapper userMapper;
    @Autowired
    PatientMapper patientMapper;
    @Autowired
    BreakdownMapper breakdownMapper;

    //用户功能
    public WkUser queryUserByName(String user_name){
        WkUser user = userMapper.queryUserByName(user_name);
        return  user;
    }

    /**
     * 注册，首先校验用户名是否存在
     * 判断
     * @param user
     * @return
     */
    public  int register(WkUser user){
        //取数据
        WkUser rUser= userMapper.queryUserByName(user.getUserName());
        //校验用户名是否存在
        if(rUser !=null &&   rUser.getUserName().equals(user.getUserName())){
            return  1;//1代表用户已存在
        }
        //注册插入数据
        int r = userMapper.register(user);
        System.out.println(r+"插入数据");
        if(r ==1){
            return 0;//成功
        }else {
            return 2;//插入不成功
        }

    }
    //登入
    @Override
    public List<WkUser> login(Map<String,Object> map) {
        return userMapper.login(map);
    }
    //查找
    @Override
    public WkUser queryUserById(int userId) {
        return userMapper.queryUserById(userId);
    }

    @Override
    public List<Breakdown> queryBreakdown(Map<String, Object> params) {
        return breakdownMapper.selectBreakdown(params);
    }
    //更新
    @Override
    public int updateUser(WkUser user) {
        int result = userMapper.updateUser(user);
        return result;
    }
    //用户分页
    @Override
    public List<WkUser> listUsersWithPagination(PageInfo pageInfo) {
        List<WkUser> users = userMapper.listUsersWithPagination(pageInfo);
        return users;
    }
    //删除
    @Override
    public int deleteUser(int userId) {
        int result = userMapper.deleteUser(userId);
        if (result == 1) {
            return 0; // 用户删除成功
        } else {
            return 2; // 用户未找到或删除失败
        }

    }
    //添加用户
    @Override
    public int insertUser(WkUser user) {
        return userMapper.insertUser(user);
    }
   //多个字段查询
    @Override
    public WkUser queryUserByFields(Map<String, Object> fields) {
        return userMapper.queryUserByFields(fields);
    }

    //菜单
    public List<Menu> queryMenuByRole(int role_id){
        List<Menu> a = userMapper.queryMenuByRole(role_id);
        System.out.println(a);
        return a;
    }

    //患者功能

    //患者分页
    @Override
    public List<Patient> listPatientWithPaginationa(PageInfo pageInfo) {
        List<Patient> patients = patientMapper.listPatientWithPaginationa(pageInfo);
        return patients;
    }
    //删除
    @Override
    public int deletePatient(int patientId) {
        int result = patientMapper.deletePatient(patientId);
        if (result == 1) {
            return 0; // 用户删除成功
        } else {
            return 2; // 用户未找到或删除失败
        }
    }
    //多个字段查询
    @Override
    public Patient queryPatientByFields(Map<String, Object> fields) {

        return patientMapper.queryPatientByFields(fields);
    }
}
